package com.rds.workflow.service.impl;


import com.rds.common.core.domain.entity.Document;
import com.rds.common.utils.DateUtils;
import com.rds.system.mapper.DocumentMapper;
import com.rds.system.service.IRdaNameCodeService;
import com.rds.workflow.domain.RdaWfTask;
import com.rds.workflow.mapper.RdaWfTaskMapper;
import com.rds.workflow.service.IRdaWfTaskService;
import com.rds.workflow.utils.WorkflowStateid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class RdaWfTaskServiceImpl implements IRdaWfTaskService {
    @Autowired
    private RdaWfTaskMapper rdaWfTaskMapper;

    @Autowired
    private DocumentMapper documentMapper;

    @Autowired
    private IRdaNameCodeService nameCodeService;

    @Override
    public RdaWfTask selectRdaWfTaskByid(Long id) {
        return rdaWfTaskMapper.selectRdaWfTaskByid(id);
    }

    @Override
    public List<RdaWfTask> selectRdaWfTaskList(RdaWfTask rdaWfTask) {
        List<RdaWfTask> list=rdaWfTaskMapper.selectRdaWfTaskList(rdaWfTask);
        return getRdaWfTasks(list);
    }

    @Override
    public List<RdaWfTask> selectRdaWfTaskByStateId(RdaWfTask rdaWfTask) {
        return rdaWfTaskMapper.selectRdaWfTaskByStateId(rdaWfTask);
    }

    @Override
    public int insertRdaWfTask(RdaWfTask rdaWfTask) {
        /*开始调用编码器 2022-06-24 start**/
        Long typeId = Long.parseLong(String.valueOf(WorkflowStateid.wfTaskTypeid));
        rdaWfTask.setTaskCode(nameCodeService.getNewCodeNameNo(typeId,""));
        /*开始调用编码器 2022-06-24 end**/
        return rdaWfTaskMapper.insertRdaWfTask(rdaWfTask);
    }

    @Override
    public int updateRdaWfTask(RdaWfTask rdaWfTask) {
        return rdaWfTaskMapper.updateRdaWfTask(rdaWfTask);
    }

    @Override
    public int updateRdaWfTaskAll(RdaWfTask rdaWfTask) {
        return rdaWfTaskMapper.updateRdaWfTaskAll(rdaWfTask);
    }

    @Override
    public int deleteRdaWfTask(Long[] ids) {
        return rdaWfTaskMapper.deleteRdaWfTask(ids);
    }

    @Override
    public List<RdaWfTask> selectRdaWfTaskByNodeid(Long nodeid,Long workflowid) {
        return rdaWfTaskMapper.selectRdaWfTaskByNodeid(nodeid,workflowid);
    }

    @Override
    public List<RdaWfTask> selectRdaWfTaskByworkflowid(RdaWfTask rdaWfTask) {
        List<RdaWfTask> list=rdaWfTaskMapper.selectRdaWfTaskByworkflowid(rdaWfTask);
        return getRdaWfTasks(list);
    }

    @Override
    public List<RdaWfTask> selectRdaWfTaskByObjectId(Long objectId, Long stateid,Long taskStateid,Long userId) {
        List<RdaWfTask> list=rdaWfTaskMapper.selectRdaWfTaskByObjectId(objectId,stateid,taskStateid,userId);
        return getRdaWfTasks(list);
    }

    /**
     * 获取对象名、耗时时长
     */
    private List<RdaWfTask> getRdaWfTasks(List<RdaWfTask> list) {
        for(RdaWfTask rdaWfTaskList:list){
            String objectName="";
            List<Document> document=documentMapper.selectDocumentByPurposeObjectid(rdaWfTaskList.getWorkflowid(),"approve");
            for(Document documentList:document){
                objectName+=documentList.getName()+",";
            }
            if(objectName!=""){
                rdaWfTaskList.setObjectName(objectName.substring(0,objectName.length()-1));
            }
            String getCostTime=rdaWfTaskList.getCostTimeDay();
            if(getCostTime!=null && !"".equals(getCostTime)){
                Integer date=Integer.parseInt(getCostTime);
                rdaWfTaskList.setCostTimeDay(DateUtils.parseTimeSeconds(date,0));
            }
        }
        return list;
    }

    @Override
    public int deleteRdaWfTaskByWorkflowid(Long workflowid) {
        return rdaWfTaskMapper.deleteRdaWfTaskByWorkflowid(workflowid);
    }

    @Override
    public int updateRdaWfTaskByStateId(Long workflowid, Long nodeid,String approveExplain, Long stateid, Date modifyTime) {
        return rdaWfTaskMapper.updateRdaWfTaskByStateId(workflowid,nodeid,approveExplain,stateid,modifyTime);
    }

    @Override
    public int updateRdaWfTaskByStatis(Long workflowid, Long stateid, String stateids) {
        return rdaWfTaskMapper.updateRdaWfTaskByStatis(workflowid,stateid,stateids);
    }

    @Override
    public int updateRdaWfTaskPersonnel(Long userId, Long nodeId, Long stateId, Long workflowId) {
        return rdaWfTaskMapper.updateRdaWfTaskPersonnel(userId,nodeId,stateId,workflowId);
    }

    @Override
    public int deleteRdaWfTaskByNodeidWorkflowid(Long nodeid, Long workflowid) {
        return rdaWfTaskMapper.deleteRdaWfTaskByNodeidWorkflowid(nodeid,workflowid);
    }
}
